package com.idconfirmsystem.dialog;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.DialogInterface.OnClickListener;
import android.os.Handler;

public class DialogHelper {
	private Context c;
	private Handler h;
	private ProgressDialog pbar = null;

	public DialogHelper(Context c) {
		this.c = c;
	}

	public DialogHelper(Context c, Handler h) {
		this.c = c;
		this.h = h;
	}

	public void displayMessageSecond(String title, String message) {
		if (this.pbar != null)
			dismiss();
		this.pbar = ProgressDialog.show(this.c, title, message, true);

		this.pbar.show();
		new Thread() {
			public void run() {
				try {
					sleep(2000L);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} finally {
					dismiss();
				}
			}
		}.start();
	}

	public void displayMessage(String title, String message) {
		if (pbar != null)
			dismiss();
		pbar = ProgressDialog.show(c, title, message, true);
		pbar.show();
	}

	public void displayMessage(String title, String message, final Runnable canCancelRun) {
		if (pbar != null)
			dismiss();
		pbar = ProgressDialog.show(c, title, message, true, true, new DialogInterface.OnCancelListener() {

			@Override
			public void onCancel(DialogInterface arg0) {
				// TODO Auto-generated method stub
				if ((h != null) && (canCancelRun != null))
					try {
						h.post(canCancelRun);
					} catch (RuntimeException e) {
						e.printStackTrace();
					}
			}

		});
		this.pbar.show();
	}

	public void dismiss() {
		if (pbar != null) {
			pbar.dismiss();
			pbar = null;
		}
	}

	public static void alert(String title, String message, Context c) {
		AlertDialog.Builder b = new AlertDialog.Builder(c);
		b.setTitle(title);
		b.setMessage(message);
		b.setPositiveButton(17039370, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
			}

		});
		b.setCancelable(true);
		b.create();
		b.show();
	}
}